package org.ktpro.shortlink.admin.controller;

import io.micrometer.core.annotation.Timed;
import lombok.RequiredArgsConstructor;
import org.ktpro.shortlink.admin.common.convention.result.Result;
import org.ktpro.shortlink.admin.common.convention.result.Results;
import org.ktpro.shortlink.admin.common.idempotent.NoRepeatSubmit;
import org.ktpro.shortlink.admin.dto.req.ShortLinkGroupSaveReqDTO;
import org.ktpro.shortlink.admin.dto.req.ShortLinkGroupSortReqDTO;
import org.ktpro.shortlink.admin.dto.req.ShortLinkGroupUpdateReqDTO;
import org.ktpro.shortlink.admin.dto.resp.ShortLinkGroupRespDTO;
import org.ktpro.shortlink.admin.service.GroupService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接分组控制层
 */
@RestController
@RequiredArgsConstructor          //构造器注入bean
public class GroupController {
    private final GroupService groupService;

    /**
     * 短链接新增分组
     */
    @NoRepeatSubmit(message = "正在创建短链分组中，请勿重复提交！")
    @Timed(value = "admin-GroupCreate", description = "短链接分组创建")
    @PostMapping("/api/short-link/admin/v1/group")
    public Result<Void> saveGroup(@RequestBody ShortLinkGroupSaveReqDTO shortLinkGroupSaveReqDTO) {
        groupService.saveGroup(shortLinkGroupSaveReqDTO.getName());
        return Results.success();
    }

    /**
     * 查询短链接分组列表
     *
     * @return 短链接分组
     */
    @Timed(value = "admin-GroupListQuery", description = "短链接分组列表查询")
    @GetMapping("/api/short-link/admin/v1/group")
    public Result<List<ShortLinkGroupRespDTO>> listGroup() {
        return Results.success(groupService.listGroup());
    }


    /**
     * 修改分组信息
     *
     * @param requestParam 传递的参数 gid 和待修改为的名称
     */
    @Timed(value = "admin-GroupUpdate", description = "短链接分组修改")
    @PutMapping("/api/short-link/admin/v1/group")
    public Result<Void> updateGroup(@RequestBody ShortLinkGroupUpdateReqDTO requestParam) {
        groupService.updateGroup(requestParam);
        return Results.success();
    }

    /**
     * 删除分组信息
     *
     * @param gid 传递的参数 gid
     */
    @Timed(value = "admin-GroupDelete", description = "短链接分组删除")
    @DeleteMapping("/api/short-link/admin/v1/group")
    public Result<Void> deleteGroup(String gid) {
        groupService.deleteGroup(gid);
        return Results.success();
    }

    /**
     * 修改分组排序
     * @param requestParam 分组排序参数(gid以及新顺序)
     */
    @PostMapping("/api/short-link/admin/v1/group/sort")
    public Result<Void> sort(@RequestBody List<ShortLinkGroupSortReqDTO> requestParam){
        groupService.sortGroup(requestParam);
        return Results.success();
    }
}
